<?php
namespace Admin\Controller;

/**
 * 新闻控制器类
 * @author Administrator
 *
 */
class NewsController extends CommController {

    // 显示数据列表
    public function newsList() {
    	// 设置搜索条件
		$condition['group_id'] = 15;
		$condition['is_delete'] = 0;
		
    	$search['title'] = I( 'post.title' );
    	if ( !empty( $search['title'] ) ) 
    		$condition['title'] = array( 'like', "%{$search['title']}%" );
		
		$search['recommend'] = (int)I( 'post.recommend', 0);
    	if ( $search['recommend'] ) 
    		$condition['recommend'] = $search['recommend']-1;
		
		$search['status'] = (int)I( 'post.status', 0);
    	if ( $search['status'] ) 
    		$condition['status'] = $search['status']-1;

        // 设置数据列表
        $model = D( 'Article' );
        $dataList = $this->getPageList( $model, $condition, 20, "create_time desc" );

		// 页面显示参数赋值
        $this->assign( "dataList", $dataList );
        $this->assign( 'search', $search );
		
        // 渲染模板
        $this->display();
    }
	
	// 视频数据列表
    public function videoList() {
    	// 设置搜索条件
		$condition['is_delete'] = 0;
		$condition['group_id'] = 16;

		$search['title'] = I( 'post.title' );
    	if ( !empty( $search['title'] ) ) 
    		$condition['title'] = array( 'like', "%{$search['title']}%" );
		
		$search['recommend'] = (int)I( 'post.recommend', 0);
    	if ( $search['recommend'] ) 
    		$condition['recommend'] = $search['recommend']-1;
		
		$search['status'] = (int)I( 'post.status', 0);
    	if ( $search['status'] ) 
    		$condition['status'] = $search['status']-1;
		
        // 设置数据列表
        $model = D( 'Article' );
        $dataList = $this->getPageList( $model, $condition, 20, "create_time desc" );
		
		
        foreach ($dataList as $k=>$v){
			$partern='/<embed.*?src="(.*?)".*?>/is';//匹配表达式，
			preg_match_all($partern, $v['content'], $result);//匹配结果
			$dataList[$k]['content'] = $result[1][0];
        }
		
		unset($result);
		
		// 页面显示参数赋值
        $this->assign( "dataList", $dataList );
        $this->assign( 'search', $search );
		
        // 渲染模板
        $this->display();
    }
	
	// 腾铭内刊数据列表
    public function documentList() {
    	// 设置搜索条件
    	// 用户名
		$condition['group_id'] = 17;
		$condition['is_delete'] = 0;
		
		$search['title'] = I( 'post.title' );
    	if ( !empty( $search['title'] ) ) 
    		$condition['title'] = array( 'like', "%{$search['title']}%" );
		
		$search['status'] = (int)I( 'post.status', 0);
    	if ( $search['status'] ) 
    		$condition['status'] = $search['status']-1;

        // 获得数据列表
        $model = D( 'Article' );
        $dataList = $this->getPageList( $model, $condition, 20, "create_time desc" );

		foreach ($dataList as $k=>$v){
			$partern='/<a.*?href="(.*?)".*?>/is';//匹配表达式，
			preg_match_all($partern, $v['content'], $result);//匹配结果
			$dataList[$k]['content'] = $result[1][0];
        }
		// 页面显示参数赋值
        $this->assign( "dataList", $dataList );
        $this->assign( 'search', $search );
		
        // 渲染模板
        $this->display();
    }

    //审核
    public function check(){
    	$idList = I( 'post.id' );
		$status = isset($_GET['status']) ? 1 : 0;
    	if ( D( 'Article' )->where( array( 'id' => array( 'in', $idList ) ) )->save( array( 'status' => $status ) ) ) {
    		$this->dwzSuccess( '审核成功', 1, 'member', 'refreshThis' );
    	} else {
    		$this->dwzSuccess( '审核失败，请重新操作', 0, 'member', 'refreshThis' );
    	}
    }

    /**
     * 新增文章资讯
     */
    public function create(){
		
		//操作类型
		$opttype = (int)I('get.opttype',0);
		
    	if (IS_POST){
    		$_POST['create_time']=time();			
    		$_POST['update_time']=time();
			$model = D ( 'Article' );
			$refresh = $_POST ['refresh'] ? $_POST ['refresh'] : 'refreshClose';
			$return = $model->commInsert ( $_POST );
			
			$actionT = $opttype == 15 ? 'newsList' : ($opttype == 16 ? 'videoList' : 'documentList');
    		if ($return) {
	    		$this->dwzSuccess( '添加成功!', 1, $actionT, 'refreshThis' );
    		}else{
    			$this->dwzSuccess( '添加失败', 0, $actionT, 'refreshThis' );
    		}
    		
    	}else{
			//获取网点列表
			$this->assign('opttype',$opttype);
    		$this->display();
    	}
    }

    // 修改文章资讯
    public function edit() {
		//操作类型
		$opttype = (int)I('get.opttype', 0);
		
    	if (IS_POST) {
			$group_id = (int)I('post.group_id', 0);
			$actionT = $group_id == 15 ? 'newsList' : ($group_id == 16 ? 'videoList' : 'documentList');

			$_POST['update_time'] = time();
    		if ( D( 'Article' )->save( $_POST ) ) {
				$this->dwzSuccess( '修改成功!', 1, $actionT, 'refreshClose' );
    		} else {
    			$this->dwzSuccess( '修改失败', 0, $actionT, 'refreshThis' );
    		}
    	} else {
    		$id = (int)I( 'get.id', 0 );
    		if ( !$id ) {
    			die( '参数id错误：'.$id );
    		}
			
			//获取网点列表
    		$dataInfo = D('Article')->where("id = {$id} and status = 0")->find();
    		$this->assign('dataInfo',$dataInfo);
			$this->assign('opttype',$opttype);
    		$this->display();
    	}
    }
	
	/**
	 * 删除一条或一组数据(伪删除)
	 */
	public function remove() {
		$idList = I ( 'post.id' );
		$modelName = CONTROLLER_NAME;
	 
		$model = D ( 'Article' );
		$return = $model->commRemove ( $idList );
		$this->dwzSuccess ( $return ['info'], $return ['status'], $modelName, "refreshThis" );
	}

}